package uoc.biblioteca.struts.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import uoc.biblioteca.mappings.dao.EstadoSolicitudHome;
import uoc.biblioteca.mappings.dao.SolicitudLlibreHome;
import uoc.biblioteca.mappings.dao.SolicitudRevistaHome;
import uoc.biblioteca.mappings.object.UserVo;
import uoc.biblioteca.struts.form.GestionSolicitudForm;
import uoc.biblioteca.util.Utils;


public class GestionHistoricsAction extends BaseAction {
    
   
	private static final Log log = LogFactory.getLog(MisSolicitudsListAction.class);
	
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
    
		GestionSolicitudForm  gestionSolicitudForm = (GestionSolicitudForm ) form;
		
		try{
			
			 //Comprobar si la session esta guardada
			UserVo userLogin = obtenerUserSession(request);
			if(userLogin==null){
				request.getSession().setAttribute("ERROR","\n" +NO_USER_SESSION);
				return mapping.findForward("errorNotUser");
			}

			boolean checkBr=false;
			boolean checkGrup=false;
			boolean checkRecer=false;
			boolean checkDes=false;
			boolean totsTipus = false;

			//Recoger filtres tipus
			if(gestionSolicitudForm.isCheckTipusBr()){
				checkBr=true;
			}
			if(gestionSolicitudForm.isCheckTipusGrup()){
				checkGrup=true;
			}
			if(gestionSolicitudForm.isCheckTipusRecer()){
				checkRecer=true;
			}
			if(gestionSolicitudForm.isCheckTipusDes()){
				checkDes=true;
			}
			
			if(!checkBr && !checkGrup && !checkRecer && !checkDes){
				totsTipus=true;
			}
			
			SolicitudLlibreHome llibreHome = new SolicitudLlibreHome();
			SolicitudRevistaHome revistaHome = new SolicitudRevistaHome();
			EstadoSolicitudHome estadoSolicitudHome = new EstadoSolicitudHome();
			
			Date dataInici = null;
			Date dataFi = null;
			
			if(gestionSolicitudForm.getDataInici()!=null){
				dataInici = Utils.tranformToDate(gestionSolicitudForm.getDataInici());
			}

			if(gestionSolicitudForm.getDataInici()!=null){
				dataFi = Utils.tranformToDate(gestionSolicitudForm.getDataFi());
			}
			
			
			//gestionSolicitudForm.setLlibres(llibreHome.getAllSolicitudsLlibreByFiltres(tots,ordre,convocada, espera, denegat, arribat, anulat,null));
			//gestionSolicitudForm.setRevistes(revistaHome.getAllSolicitudsRevistaByFiltres(tots, ordre, convocada, espera, denegat, arribat, anulat,null));
			gestionSolicitudForm.setEstadosSolicituds(estadoSolicitudHome.getAllEstatsGestion());
			gestionSolicitudForm.setLlibres(llibreHome.getAllSolicitudsLlibreByFiltres(gestionSolicitudForm.getIdEstadoSolicitudSelectSearch(),totsTipus,checkBr,checkGrup,checkRecer,checkDes,dataInici,dataFi));
			gestionSolicitudForm.setRevistes(revistaHome.getAllSolicitudsRevistaByFiltres(gestionSolicitudForm.getIdEstadoSolicitudSelectSearch(),dataInici,dataFi));
			request.getSession().setAttribute("listaEstados", estadoSolicitudHome.getAllEstatsComboGestion());
			
			
			return mapping.findForward("gestioHistoricsList");
		}
		catch (Exception e) {
			e.printStackTrace();
			log.error("Error: ", e);
			request.getSession().setAttribute("ERROR","\n" +e.toString()  );
			return mapping.findForward("error");
		}
	}
}